using B.T.BasicData.ErrorCode;
using B.T.BasicData.Read.Api.DTOs;
using B.T.BasicData.Read.Api.RBACServices.IServices;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace B.T.BasicData.Read.Api.Controllers
{
    /// <summary>
    /// 用户登录控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class LoginController : ControllerBase
    {
        private readonly ILoginServices _loginServices;
        private readonly ILogger<LoginController> logger;

        public LoginController(ILoginServices loginServices,  ILogger<LoginController> logger)
        {
            _loginServices = loginServices;
            this.logger = logger;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="loginDto">登录信息</param>
        /// <returns>登录结果</returns>
        [HttpPost]
        public async Task<ApiResult<LoginResultDto>> Login([FromBody] LoginDto loginDto)
        {
            try
            {
                logger.LogInformation($"收到登录请求: {loginDto.UserName}");
                return await _loginServices.Login(loginDto);
            }
            catch (Exception ex)
            {
                logger.LogError($"登录接口异常: {ex.Message}");
                var result = new ApiResult<LoginResultDto>();
                result.msg = "登录过程中发生异常";
                return result;
            }
        }

        
    }
} 